package com.lc.rongzecaifu.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lc.rongzecaifu.activity.InvestmentActivity;
import com.lc.rongzecaifu.conn.JsonIndex;
import com.lc.rongzecaifu.progress.SaundProgressBar;
import com.lc.rongzecaifu.rongzecaifu.R;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/12/12.
 */
public class HomeListRecyAdapter extends RecyclerView.Adapter<HomeListRecyAdapter.ViewHolder> {

    private Context context;
    private List<JsonIndex.Info.Borrow_list> list = new ArrayList<>();

    public HomeListRecyAdapter(Context context, List<JsonIndex.Info.Borrow_list> list) {
        this.context = context;
        this.list = list;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        @Bind(R.id.tv_home_list_title)
        TextView tvRate;
        @Bind(R.id.tv_home_list_desc)
        TextView tvTime;

        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_sumMoney)
        TextView tvSumMoney;
        @Bind(R.id.regularprogressbar)
        SaundProgressBar mPbar;

        public String str;
        public int a;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

/*    @Override
    public HomeListRecyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_home_list, null);
        HomeListRecyAdapter.ViewHolder vh = new HomeListRecyAdapter.ViewHolder(view);
        return vh;
    }*/


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_home_list, null);
        HomeListRecyAdapter.ViewHolder vh = new HomeListRecyAdapter.ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (list.size()==0){

        }else {

            holder.tvName.setText(list.get(position).borrow_name);
            NumberFormat nfNumber = NumberFormat.getNumberInstance();
            holder.tvRate.setText("年化率  " + list.get(position).borrow_interest_rate + "%");
            holder.tvTime.setText("期限  " + list.get(position).borrow_duration + list.get(position).borrow_duration_cn);


            holder.tvSumMoney.setText(list.get(position).need);
            holder.mPbar.setMax(100);
            final Drawable indicator = context.getResources().getDrawable(
                    R.mipmap.jindu4);
            Rect bounds = new Rect(0, 0, indicator.getIntrinsicWidth(),
                    indicator.getIntrinsicHeight());
            indicator.setBounds(bounds);
            holder.mPbar.setProgressIndicator(indicator);
            holder.mPbar.setVisibility(View.VISIBLE);
            String string = list.get(position).progress;
            holder.mPbar.setmText(string);
            int a;
            holder.mPbar.setProgress((int) Float.parseFloat(list.get(position).progress));
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (list.isEmpty() | list.size() == 0) {

                    } else {
                        Intent intent = new Intent();
                     //   intent.putExtra("id", list.get(position).id);

                        intent.putExtra("id",list.get(position).id);
                        intent.setClass(context, InvestmentActivity.class);
                        context.startActivity(intent);
                    }


                }
            });
        }

    }



    @Override
    public int getItemCount() {
        return list.size();
    }

/*
    @Override
    public int getCount() {
        return menuListContent.size();
    }

    @Override
    public Object getItem(int position) {
        return menuListContent.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;

        if (convertView == null) {

            convertView = LayoutInflater.from(context).inflate(R.layout.item_home_list, null);

//            MyApplication.scaleScreenHelper.loadView((ViewGroup) convertView);

            viewHolder = new ViewHolder();

            viewHolder.tv_home_list_title = (TextView) convertView.findViewById(R.id.tv_home_list_title);

            viewHolder.tv_home_list_desc = (TextView) convertView.findViewById(R.id.tv_home_list_desc);


            viewHolder.tv_sumMoney = (TextView) convertView.findViewById(R.id.tv_sumMoney);
            viewHolder.mPbar = (SaundProgressBar) convertView.findViewById(R.id.regularprogressbar);
            viewHolder.mPbar.setMax(100);
            Drawable indicator = context.getResources().getDrawable(
                    R.mipmap.jindu4);
            Rect bounds = new Rect(0, 0, indicator.getIntrinsicWidth(),
                    indicator.getIntrinsicHeight());
            indicator.setBounds(bounds);

            viewHolder.mPbar.setProgressIndicator(indicator);
            viewHolder.mPbar.setmText(menuListContent.get(position).progress);
            viewHolder.mPbar.setVisibility(View.VISIBLE);
            String str_progress=menuListContent.get(position).progress;

            Double d=Double.valueOf(str_progress);


            viewHolder.mPbar.setProgress(d.intValue());


            viewHolder.tv_name= (TextView) convertView.findViewById(R.id.tv_name);



            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();

        }

        //年华利率
        viewHolder.tv_home_list_title.setText("预计年化收益率"+menuListContent.get(position).borrow_interest_rate+"%");

        //期限
        viewHolder.tv_home_list_desc.setText("期限"+menuListContent.get(position).borrow_duration+menuListContent.get(position).borrow_duration_cn);
        //首页项目名称
        viewHolder.tv_name.setText(menuListContent.get(position).borrow_name);
        viewHolder.tv_sumMoney.setText(menuListContent.get(position).need);



        return convertView;

    }

    private class ViewHolder {

        public TextView tv_home_list_title, tv_home_list_desc;

        public SaundProgressBar mPbar;


        public TextView tv_sumMoney;

        //首页标题
        public TextView tv_name;




    }*/

}
